<?php
namespace Core;
use App\Library\TextLog;
use App\Logger;

class TBServer extends \Swoole\Server
{
    public $listener;
	/**
	 * 构造方法
	 * @param string $ip 绑定IP
	 * @param int $port 监听端口
	 */
	public function __construct($ip,$port)
	{
		parent::__construct($ip, $port);
	}

	/*public function send($fd, $send_data, $reactor_id = null)
    {
        global $handler;

         // 测试使用，记录发送的流量(单位b)
        if (method_exists($handler, 'getFlow')) {
            $flow = $handler->getFlow();
            $flow->incr($fd, 'send', strlen(bin2hex($send_data)) / 2);
        }

        if (method_exists($handler, 'getLoggerInfo')) {
            $loggerInfo = $handler->getLoggerInfo($fd);

            if ($loggerInfo['tbMac']) {
                $redis = TbRedis::init($this->worker_id);
                // 获取当前发送流量，并累加
                $tbKey = Logger::tb($loggerInfo['tbMac']);

                $hashKey = $loggerInfo['type'] . 'SendBytes';
                $hasSendBytes = $redis->hGet($tbKey, $hashKey) ?: 0;

                // 本次发送的流量
                $curSendBytes = strlen(bin2hex($send_data)) / 2;
                $hasSendBytes += $curSendBytes;

                $method = $loggerInfo['method'];
                TextLog::$method($loggerInfo['tbMac'])->debug('send:', [
                    'fd' => $fd,
                    'cur' => $curSendBytes . 'B',
                    'all' => number_format($hasSendBytes / 1024, 3) . 'KB',
                ]);
                $redis->hSet($tbKey, $hashKey, $hasSendBytes);
            }
        }

        return parent::send($fd, $send_data, $reactor_id); // TODO: Change the autogenerated stub
    }*/

    /**
     * 启动服务
     * @param $handler
     * @param array $config
     */
	public function run($handler,  array $config)
	{
		$this->set($config);
		if (defined('ORDER_IP') && defined('ORDER_PORT')) {
            $this->listener= $this->addlistener(ORDER_IP,ORDER_PORT,SWOOLE_SOCK_TCP);
        }
        
        $this->on('start', array($handler, 'onStart'));
        $this->on('ManagerStart', array($handler, 'onManagerStart'));
		$this->on('WorkerStart', array($handler, 'onWorkerStart'));
        $this->on('WorkerStop', array($handler, 'onWorkerStop'));
		$this->on('connect', array($handler, 'onConnect'));
		$this->on('receive', array($handler, 'onReceive'));
		$this->on('close', array($handler, 'onClose'));
        $this->on('shutdown', array($handler, 'onShutdown'));
        $this->on('Task', array($handler, 'onTask'));
        $this->on('Finish', array($handler, 'onFinish'));

        if (defined('ORDER_IP') && defined('ORDER_PORT')) {
            $this->listener->set(config('listener'));
            $this->listener->on('connect', array($handler, 'onListenerConnect'));
            $this->listener->on('receive', array($handler, 'onListenerReceive'));
            $this->listener->on('close', array($handler, 'onListenerClose'));
        }

        if (method_exists($handler, 'getProcess')) {
            if ($process = $handler->getProcess($this)) {
                foreach ($process as $p) {
                    $this->addProcess($p);
                }
            }
        }

      	$this->start();
	}
}